﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
using Luna小店.UC.套号管理.套号吊牌.service;
using Luna小店.service;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraLayout;
using DevExpress.XtraGrid.Editors;
using DevExpress.Utils.Win;

namespace Luna小店.UC.套号管理.套号吊牌
{
    public partial class Form套号吊牌模板 : DevExpress.XtraEditors.XtraForm
    {
        private string id;
        private String name;
        public Form套号吊牌模板()
        {
            InitializeComponent();
            this.searchLookUpEdit1.Properties.DataSource = 套号吊牌Service.get款式大类别();
        }

        #region 选择款式
        private void searchLookUpEdit1_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clearBtn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit1View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit1.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    this.id = this.searchLookUpEdit1.Properties.View.GetRowCellValue(rowHandle, "id").ToString();
                    this.name = this.searchLookUpEdit1.Properties.View.GetRowCellValue(rowHandle, "name").ToString();
                }
            }
       }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clearBtn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit1.ToolTip = null;
            searchLookUpEdit1.EditValue = null;
        }
        private void searchLookUpEdit1_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.name;
        }
        #endregion

        private void simpleButton套号吊牌模板上传_Click(object sender, EventArgs e)
        {
            //上传文件
            OpenFileDialog ofd = new OpenFileDialog();
            //ofd.Filter = "pdf,doc,docx|*.pdf;*.doc;*.docx;*.jpg;";
            ofd.Title = "请选择上传文件";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                套号吊牌Service.post套号模板(ofd,this.id);
                MessageBox.Show("操作成功");
                this.Close();
            }
        }
    }
}